package top.ewind.controller.wx;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Map;

import org.json.JSONObject;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.ApiResult;

import top.ewind.util.SecurityUtil;

public class LoginWx extends Controller {
	public static String identifier;
	public void index(){
		render("/aigo/login.html");
	}
	/*验证码*/
	public void yan(){
		String phonenumber = getPara("username");
		String urlstr = "http://api.life.aigo.com/sendVerifyCode.html?action=sendVerifyCode&phone="+phonenumber+"2013-03-29sendVerifyCode";
		String sing = SecurityUtil.md5(urlstr).toLowerCase();			
		String jsss = "action=sendVerifyCode&phone="+phonenumber+"&sign="+sing;
		ApiResult us = UserApi.sendverifycode(jsss);
		String resu =us.getStr("identifier");
		if(resu!=null){
			identifier = resu;
			renderJson("vocod","1");
		}else{
			renderJson("vocod","发送失败");
		}
		
	}
	/*用户是否存在*/
	public void UserNameExist(){
		String phonenumber = getPara("username");
		String sgin = SecurityUtil.md5("http://api.aigolife.com/if/account.jhtml?action=UserNameExist&username="+phonenumber+"2013-03-29UserNameExist").toLowerCase();
		String jsss = "action=UserNameExist&username="+phonenumber+"&sign="+sgin;
		ApiResult us = UserApi.UserNameExist(jsss);
		Boolean resu =us.getBoolean("exist");
		if(resu!=null){
			renderJson("resu",resu);
		}else{
			renderJson("resu",false);
		}
	}
	/*用户注册*/
	public void wxreg(){
		String phonenumber = getPara("username");
		String pwd = getPara("password");
		String vcode = getPara("vcode");
		String nid = "1";
		String ident = identifier;
		String username="{username:\""+phonenumber+"\"}";
		String pwdsgin = SecurityUtil.md5(pwd).toLowerCase();
		String md5str = "http://api.aigolife.com/if/account.jhtml?action=UserRegister2&user="+username+"&password="+pwdsgin+"&verifyCode="+vcode+"&identifier="+ident+"2013-03-29UserRegister2";
		String sgin = SecurityUtil.md5(md5str).toLowerCase();
		String jsss = "action=UserRegister2&user="+username+"&password="+pwdsgin+"&verifyCode="+vcode+"&identifier="+ident+"&sign="+sgin;
		ApiResult us = UserApi.account(jsss);
		Map<?, ?> sus = us.getMap("result");
		Boolean s = (Boolean) sus.get("result");
		if(s==true){
			Record deviceuser =new Record().set("nid", nid).set("username", phonenumber).set("password", pwdsgin).set("stat", "1").set("intime", new Date()).set("userkey",jsss);
			Db.save("device_user", deviceuser);
			renderJson("msg","1");
		}else{
			renderJson("msg",sus.get("reason"));
		}
		
	}
	/*登录*/
	public void wxlogin(){
		String phonenumber = getPara("username");
		String pwd = getPara("password");
		String action = "UserLogin";
		String pwdsgin = SecurityUtil.md5(pwd).toLowerCase();
		String sgin = SecurityUtil.md5("http://api.aigolife.com/if/account.jhtml?action=UserLogin&username="+phonenumber+"&password="+pwdsgin+"2013-03-29"+action).toLowerCase();
		String jsss = "action=UserLogin&username="+phonenumber+"&password="+pwdsgin+"&sign="+sgin;
		ApiResult us = UserApi.account(jsss);
		Map<?, ?> sus = us.getMap("result");
		Boolean s = (Boolean) sus.get("result");
		Record sysuser = null;
		if(s==true){
			sysuser =Db.findFirst("select t.* from device_user t where t.username = ? and t.password = ?", phonenumber,pwdsgin);
			if(sysuser!=null&&sysuser.getInt("stat").intValue()==1){
				setSessionAttr("sysuser",sysuser);
				setSessionAttr("nid",sysuser.getInt("nid"));
				renderJson("msg","success");
			}
			else{
				renderJson("msg","用户名或密码不正确！");
			}
		}else{
			renderJson("msg",sus.get("reason"));
		}		
	}
	/*public void wxlogins(){
		String pwd = getPara("password");
		if(StrKit.isBlank(getPara("username"))||StrKit.isBlank(pwd)){
			renderJson("msg","用户和密码不能为空！");
		}
		else{
			
			String username = SecurityUtil.sqlFilter(getPara("username"));
			String epass = SecurityUtil.md5(pwd);
			Record sysuser = null;
			
				sysuser =Db.findFirst("select t.* from device_user t where t.username = ? and t.password = ?", username,epass);
			
			if(sysuser!=null&&sysuser.getInt("stat").intValue()==1){
				setSessionAttr("sysuser",sysuser);
				setSessionAttr("nid",sysuser.getInt("nid"));
				renderJson("msg","success");
			}
			else{
				renderJson("msg","用户名或密码不正确！");
			}
		}
	}*/
}